/*
    Copyright (c) 2009 Pavol Malosek a.k.a. malo

    This file is part of ComComLib.

    ComComLib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ComComLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <lcc/api.hpp>

#include <assert.h>
#include <unistd.h>
#include <iostream>

void wait_for_ready (lcc::api_t *api, unsigned int logger_id_)
{
    //  Wait for device to become ready.
    while (true) {
        if (!api->ready (logger_id_))
            sleep (1);
        else 
            break;
    }
}

int main (int argv, char *argc [])
{

    if (argv != 2) {
        std::cerr << "Usage: logger_memory ttyname" << std::endl;
        return 1;
    }

    lcc::api_t api;
    
    int room_logger = api.create_logger (lcc::Rxxxx, argc [1]);
    assert (room_logger >= 0);

    while (true) {

        //  Check for logger presence.
        api.touch_logger (room_logger);

        wait_for_ready (&api, room_logger);

        if (!api.touch_logger_ok (room_logger)) {
            std::cout << "Could not communicate with logger." << std::endl;
            break;
        }

        std::cout << "Logger " << room_logger << " is alive." << std::endl;

        //  Read logger service memory
        api.read_service_memory (room_logger);

        wait_for_ready (&api, room_logger);

        if (!api.read_service_memory_ok (room_logger)) {
            std::cout << "Could not read service memory." << std::endl;
            break;
        }

        std::cout << "Service memory read from logger " << 
            room_logger << " ok." << std::endl;

        lcc::device_info_t info;

        if (!api.get_device_info (room_logger, &info)) {
            std::cout << "Could not fill logger info." << std::endl;
            break;
        }

        lcc::print_device_info (&info);

        std::cout << "Press enter to read records from logger "
            << room_logger << ", it may take a while!" << std::endl;

        char key = getchar ();

        //  Read records
        api.read_records (room_logger);

        wait_for_ready (&api, room_logger);

        if (!api.read_records_ok (room_logger)) {
            std::cout << "Could not read records." << std::endl;
            break;
        }

        std::cout << "Data records from logger " << room_logger << " ok." << 
            std::endl;

        std::list<lcc::record_t> records = api.get_records (room_logger);

        std::cout << "Have " << records.size() << " records." << std::endl;
      
        std::cout << "Press enter to print records." << std::endl;
        key = getchar ();

        std::list<lcc::record_t>::iterator it = records.begin ();

        while (it != records.end()) {
            lcc::print_record (&*it);
            it++;
        }

        break;
    }

    api.destroy_logger (room_logger);

    return 0;
}
